﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;

using NHibernate.Criterion;

namespace Twipler.Data.Access
{
    public class DaoVisitor : AbstractDaoDataEntity<Data.Entity.Twipler.Visitor, int> { }

    public class DaoAlias  : AbstractDaoDataEntity<Data.Entity.Twipler.Alias, int> { }

    public class DaoSetting : AbstractDaoDataEntity<Data.Entity.Twipler.Setting, int> 
    {
        public string GetSetting(string name, Data.Entity.Twipler.SettingType type)
        {
            List<Data.Entity.Twipler.Setting> settings = Factory.Setting.GetByCriteria(
                    Expression.And(
                    Expression.Eq("Code", name),
                    Expression.Eq("SettingType", (int)type)));

            if (settings.Count > 0)
                return settings[0].SettingValue;

            return null;
        }

        internal int GetSettingInt(string name, Data.Entity.Twipler.SettingType type, int defaultValue)
        {
            string str = GetSetting(name, type);
            if (String.IsNullOrEmpty(str))
                return defaultValue;

            int i;
            if (int.TryParse(str, out i))
                return i;

            return defaultValue;
        }

        internal bool GetSettingBool(string name, Data.Entity.Twipler.SettingType settingType, bool defaultValue)
        {
            int i = GetSettingInt(name, settingType, -1);
            if (i == -1)
                return defaultValue;

            return (i != 0);            
        }
    }

    public partial class Factory
    {
        public static Data.Access.DaoVisitor Visitor
        {
            get { return new Data.Access.DaoVisitor(); }
        }

        public static Data.Access.DaoAlias Alias
        {
            get { return new Data.Access.DaoAlias(); }
        }

        public static Data.Access.DaoSetting Setting
        {
            get { return new Data.Access.DaoSetting(); }
        }
    }
}